package com.example.x50.untils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Post2 {
    public static void send(String s) {
        System.out.println("准备发送的数据"+s);
        String result = "";
        try {
            //示例url
            String url = "http://192.168.1.126:8080/ruff/reception/ruff_data";
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "application/octet-stream");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/octet-stream");
            conn.setRequestProperty("Authorization", "Basic YWRtaW46cnVmZg==");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //发送文件
//            File file = new File("C:\\Users\\rp\\Desktop\\conf.json");
//            byte[] Bytes = new byte[(int)file.length()] ;
            // 发送请求参数
            DataOutputStream dos=new DataOutputStream(conn.getOutputStream());

//            InputStream os =  new FileInputStream(file);
            //发送字符串
            String content=s;
            System.out.println(content);
//            os.read(Bytes);
//            System.out.println(Bytes[1]);
            dos.write(content.getBytes());

            // flush输出流的缓冲
            dos.flush();
            // 定义BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println(result);//打印输出结果
        } catch (Exception e) {
            System.out.println("异常," + e.getMessage());
            System.out.println("网络异常");
            e.printStackTrace();
        }
    }

    }
